package cn.aitrox.ry.service.auth.service.impl;

import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.enumtype.RespCodeEnum;
import cn.aitrox.ry.service.auth.dao.AuthGroupDao;
import cn.aitrox.ry.service.auth.dao.AuthGroupRoleDao;
import cn.aitrox.ry.service.auth.dao.AuthGroupUserDao;
import cn.aitrox.ry.service.auth.dao.AuthRoleDao;
import cn.aitrox.ry.service.auth.dao.AuthUserRoleDao;
import cn.aitrox.ry.service.auth.dto.group.in.AuthGroupCreateInDto;
import cn.aitrox.ry.service.auth.dto.group.in.AuthGroupUpdateInDto;
import cn.aitrox.ry.service.auth.dto.group.out.AuthGroupDetailDto;
import cn.aitrox.ry.service.auth.dto.role.out.AuthRoleDetailDto;
import cn.aitrox.ry.service.auth.entity.AuthGroupEntity;
import cn.aitrox.ry.service.auth.entity.AuthGroupRoleEntity;
import cn.aitrox.ry.service.auth.entity.AuthGroupUserEntity;
import cn.aitrox.ry.service.auth.entity.AuthRoleEntity;
import cn.aitrox.ry.service.auth.entity.AuthUserRoleEntity;
import cn.aitrox.ry.service.auth.service.AuthGroupService;
import cn.aitrox.ry.service.auth.service.AuthRoleService;
import cn.aitrox.ry.util.ArrayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AuthGroupServiceImpl implements AuthGroupService {

    @Autowired
    private AuthGroupDao authGroupDao;

    @Autowired
    private AuthGroupRoleDao authGroupRoleDao;

    @Autowired
    private AuthGroupUserDao authGroupUserDao;

    @Autowired
    private AuthRoleDao authRoleDao;

    @Autowired
    private AuthUserRoleDao authUserRoleDao;

    @Autowired
    private AuthRoleService authRoleService;

    @Transactional
    @Override
    public RespResult<Integer> create(AuthGroupCreateInDto input) {

        String name = input.getName();
        List<Integer> roleIds = input.getRoleIds();
        List<Integer> userIds = input.getUserIds();

        // 校验名称是否重复
        if (null != authGroupDao.findOne(new AuthGroupEntity(null, name))) {
            return RespResult.secByError(RespCodeEnum.EXISTED.getCode(), "组名称已存在");
        }

        // 判断角色是否存在
        RespResult respResult = authRoleService.validRoleExist(roleIds);
        if (!respResult._isOk()) {
            return respResult;
        }

        // 保存group、group_role、group_user、user_role， 用户可能已经有了角色，所以要排除已经有的角色，不重复插入

        AuthGroupEntity group = new AuthGroupEntity(null, name);
        authGroupDao.insertSelective(group);

        if (!ArrayUtil.isEmpty(userIds)) {
            List<AuthGroupUserEntity> groupUserList = userIds.stream().map(userId -> new AuthGroupUserEntity(null, group.getId(), userId)).collect(Collectors.toList());
            authGroupUserDao.batchInsert(groupUserList);
        }

        if (!ArrayUtil.isEmpty(roleIds)) {
            List<AuthGroupRoleEntity> groupRoleList = roleIds.stream().map(roleId -> new AuthGroupRoleEntity(null, group.getId(), roleId)).collect(Collectors.toList());
            authGroupRoleDao.batchInsert(groupRoleList);
        }

        if (!ArrayUtil.isEmpty(userIds) && !ArrayUtil.isEmpty(roleIds)) {
            List<AuthUserRoleEntity> userRoleList = new ArrayList<>();
            List<AuthUserRoleEntity> userRoleListInDB = authUserRoleDao.findByUserIds(userIds);
            Map<Integer, List<Integer>> userIdRoleListMap = userRoleListInDB.stream().collect(Collectors.groupingBy(AuthUserRoleEntity::getUserId, Collectors.mapping(AuthUserRoleEntity::getRoleId, Collectors.toList())));

            for (Integer userId : userIds) {
                List<Integer> roleIdsByUserId = null == userIdRoleListMap.get(userId) ? new ArrayList<>() : userIdRoleListMap.get(userId);
                List<AuthUserRoleEntity> userRolesByUserId = roleIds.stream().filter(roleId -> !roleIdsByUserId.contains(roleId)).map(roleId -> new AuthUserRoleEntity(null, userId, roleId)).collect(Collectors.toList());

                if (!ArrayUtil.isEmpty(userRolesByUserId)) {
                    userRoleList.addAll(userRolesByUserId);
                }
            }

            if (!ArrayUtil.isEmpty(userRoleList)) {
                authUserRoleDao.batchInsert(userRoleList);
            }
        }

        return RespResult.ok(group.getId());
    }

    @Transactional
    @Override
    public RespResult update(AuthGroupUpdateInDto input) {

        Integer groupId = input.getId();
        List<Integer> roleIds = input.getRoleIds();
        List<Integer> userIds = input.getUserIds();

        // 校验用户组是否存在
        AuthGroupEntity authGroupEntity = authGroupDao.findById(groupId);
        if (null == authGroupEntity) {
            return RespResult.secByError(RespCodeEnum.NOT_EXIST.getCode(), "用户组不存在");
        }

        // 校验用户组名称是否存在
        if (null != input.getName()) {
            AuthRoleEntity authRoleEntityByName = authRoleDao.findOne(new AuthRoleEntity(null, input.getName()));
            if (null != authRoleEntityByName && !groupId.equals(authRoleEntityByName.getId())) {
                return RespResult.secByError(RespCodeEnum.EXISTED.getCode(), "用户组名称已存在");
            }
        }

        List<AuthGroupUserEntity> groupUserListInDB = authGroupUserDao.findByCondition(new AuthGroupUserEntity(null, groupId, null), null);
        List<AuthGroupRoleEntity> groupRoleListInDB = authGroupRoleDao.findByCondition(new AuthGroupRoleEntity(null, groupId, null), null);

        List<Integer> userIdsInDB = groupUserListInDB.stream().map(AuthGroupUserEntity::getUserId).distinct().collect(Collectors.toList());
        List<Integer> roleIdsInDB = groupRoleListInDB.stream().map(AuthGroupRoleEntity::getRoleId).distinct().collect(Collectors.toList());

        // 找出哪些用户是新增、删除的
        List<Integer> userIdsAdd = userIds.stream().filter(userId -> !userIdsInDB.contains(userId)).collect(Collectors.toList());
        List<Integer> userIdsRemove = userIdsInDB.stream().filter(userId -> !userIds.contains(userId)).collect(Collectors.toList());

        // 找出哪些角色是新增、删除的
        List<Integer> roleIdsAdd = roleIds.stream().filter(roleId -> !roleIdsInDB.contains(roleId)).collect(Collectors.toList());
        List<Integer> roleIdsRemove = roleIdsInDB.stream().filter(roleId -> !roleIds.contains(roleId)).collect(Collectors.toList());

        List<AuthGroupUserEntity> groupUserAdd = null;
        List<AuthGroupRoleEntity> groupRoleAdd = null;

        if (!ArrayUtil.isEmpty(userIdsAdd)) {
            groupUserAdd = userIdsAdd.stream().map(userId -> new AuthGroupUserEntity(null, groupId, userId)).collect(Collectors.toList());
        }

        if (!ArrayUtil.isEmpty(roleIdsAdd)) {
            groupRoleAdd = roleIdsAdd.stream().map(roleId -> new AuthGroupRoleEntity(null, groupId, roleId)).collect(Collectors.toList());
        }

        // 相关联的用户 组关联的用户 + 新增的用户
        List<Integer> relationUserIds = new ArrayList<>();
        relationUserIds.addAll(userIdsInDB);
        relationUserIds.addAll(userIdsAdd);
        relationUserIds = relationUserIds.stream().distinct().collect(Collectors.toList());

        // 所有关联用户的角色
        Map<Integer, List<Integer>> relationuUerIdRoleIdsMap = authUserRoleDao.findByUserIds(relationUserIds).stream().collect(Collectors.groupingBy(AuthUserRoleEntity::getUserId, Collectors.mapping(AuthUserRoleEntity::getRoleId, Collectors.toList())));

        // 将所有相关的用户角色进行重组
        if (!ArrayUtil.isEmpty(relationUserIds)) {
            // 查询关联用户的组用户信息、组角色
            List<AuthGroupUserEntity> groupUsersByRelation = authGroupUserDao.findByUserIds(relationUserIds);
            List<AuthGroupRoleEntity> groupRolesByRelation = authGroupRoleDao.findByGroupIds(groupUsersByRelation.stream().map(AuthGroupUserEntity::getGroupId).distinct().collect(Collectors.toList()));

            // 得到用户对应的组列表
            Map<Integer, List<Integer>> userIdGrouoIdsMapByRelation = groupUsersByRelation.stream().collect(Collectors.groupingBy(AuthGroupUserEntity::getUserId, Collectors.mapping(AuthGroupUserEntity::getGroupId, Collectors.toList())));

            // 得到组对应的角色列表
            Map<Integer, List<Integer>> groupIdRoleIdsMapByRelation = groupRolesByRelation.stream().collect(Collectors.groupingBy(AuthGroupRoleEntity::getGroupId, Collectors.mapping(AuthGroupRoleEntity::getRoleId, Collectors.toList())));

            // 筛选出用户在非当前组所拥有的角色
            Map<Integer, List<Integer>> userIdRoleIdsMapExcludeCurrentGroup = getUserIdRoleIdsMapExcludeCurrentGroup(groupId, userIdGrouoIdsMapByRelation, groupIdRoleIdsMapByRelation);

            // 新增或者删除 user_role
            List<AuthUserRoleEntity> userRolesAdd = new ArrayList<>();
            Map<Integer, List<Integer>> userIdRoleIdsRemoveMap = new HashMap<>();

            // 新增用户的情况
            if (!ArrayUtil.isEmpty(userIdsAdd) && !ArrayUtil.isEmpty(roleIds)) {
                userRolesAdd.addAll(userIdsAdd.stream().map(userId -> {
                    // 排除数据库已经存在的用户角色
                    List<Integer> tmpRoleIds = null == relationuUerIdRoleIdsMap.get(userId) ? new ArrayList<>() : relationuUerIdRoleIdsMap.get(userId);
                    return roleIds.stream().filter(roleId -> !tmpRoleIds.contains(roleId)).map(roleId -> new AuthUserRoleEntity(null, userId, roleId)).collect(Collectors.toList());
                }).flatMap(Collection::stream).collect(Collectors.toList()));
            }


            // 删除用户的情况，需要删除用户的该组对应的角色，但是要排除其他组有相同的角色
            if (!ArrayUtil.isEmpty(userIdsRemove)) {
                List<Integer> originRoleIdsByGroupId = groupIdRoleIdsMapByRelation.get(groupId);
                List<Integer> removeRoleIds = new ArrayList<>();
                if (!ArrayUtil.isEmpty(originRoleIdsByGroupId)) {
                    removeRoleIds.addAll(originRoleIdsByGroupId);
                }
                if (!ArrayUtil.isEmpty(roleIdsRemove)) {
                    removeRoleIds.addAll(roleIdsRemove);
                }
                removeRoleIds = removeRoleIds.stream().distinct().collect(Collectors.toList());
                if (!ArrayUtil.isEmpty(removeRoleIds)) {
                    // 用户在其他组拥有的角色
                    for (Integer userId : userIdsRemove) {
                        List<Integer> grougIdsByUserIdInOtherGroup = null == userIdRoleIdsMapExcludeCurrentGroup.get(userId) ? new ArrayList<>() : userIdRoleIdsMapExcludeCurrentGroup.get(userId);
                        List<Integer> roleIdsNeedRemoveByUserId = removeRoleIds.stream().filter(roleId -> !grougIdsByUserIdInOtherGroup.contains(roleId)).collect(Collectors.toList());
                        if (!ArrayUtil.isEmpty(roleIdsNeedRemoveByUserId)) {
                            userIdRoleIdsRemoveMap.put(userId, roleIdsNeedRemoveByUserId);
                        }
                    }
                }
            }

            // 新增角色的情况
            if (!ArrayUtil.isEmpty(roleIdsAdd)) {
                userRolesAdd.addAll(relationUserIds.stream().map(userId -> {
                    // 排除数据库已经存在的用户角色
                    List<Integer> tmpRoleIds = null == relationuUerIdRoleIdsMap.get(userId) ? new ArrayList<>() : relationuUerIdRoleIdsMap.get(userId);
                    return roleIdsAdd.stream().filter(roleId -> !tmpRoleIds.contains(roleId)).map(roleId -> new AuthUserRoleEntity(null, userId, roleId)).collect(Collectors.toList());
                }).flatMap(Collection::stream).collect(Collectors.toList()));
            }

            // 删除角色的情况，需要删除所有关联用户的该组对应的角色，但是要排除其他组有相同的角色
            if (!ArrayUtil.isEmpty(roleIdsRemove) && !ArrayUtil.isEmpty(relationUserIds)) {
                List<Integer> removeRoleIds = new ArrayList<>();
                if (!ArrayUtil.isEmpty(roleIdsRemove)) {
                    removeRoleIds.addAll(roleIdsRemove);
                }
                removeRoleIds = removeRoleIds.stream().distinct().collect(Collectors.toList());
                if (!ArrayUtil.isEmpty(removeRoleIds)) {
                    // 用户在其他组拥有的角色
                    for (Integer userId : relationUserIds) {
                        List<Integer> grougIdsByUserIdInOtherGroup = null == userIdRoleIdsMapExcludeCurrentGroup.get(userId) ? new ArrayList<>() : userIdRoleIdsMapExcludeCurrentGroup.get(userId);
                        List<Integer> roleIdsNeedRemoveByUserId = removeRoleIds.stream().filter(roleId -> !grougIdsByUserIdInOtherGroup.contains(roleId)).collect(Collectors.toList());
                        if (!ArrayUtil.isEmpty(roleIdsNeedRemoveByUserId)) {
                            List<Integer> roleIdsNeedRemoveByUserIdExised = userIdRoleIdsRemoveMap.get(userId);
                            if (null != roleIdsNeedRemoveByUserIdExised) {
                                roleIdsNeedRemoveByUserId.addAll(roleIdsNeedRemoveByUserIdExised);
                                roleIdsNeedRemoveByUserId = roleIdsNeedRemoveByUserId.stream().distinct().collect(Collectors.toList());
                            }
                            userIdRoleIdsRemoveMap.put(userId, roleIdsNeedRemoveByUserId);
                        }
                    }
                }
            }

            // 新增的数据进行去重
            userRolesAdd = userRolesAdd.stream().distinct().collect(Collectors.toList());


            // 保存删除user_role
            for (Integer userId : userIdRoleIdsRemoveMap.keySet()) {
                List<Integer> roleIdsRemoveByUserId = userIdRoleIdsRemoveMap.get(userId);
                if (ArrayUtil.isEmpty(roleIdsRemoveByUserId)) {
                    continue;
                }
                authUserRoleDao.deleteByUserIdRoleIds(userId, roleIdsRemoveByUserId);
            }

            authUserRoleDao.batchInsert(userRolesAdd);
        }


        // 删除或者新增 group_user、group_role、user_role
        if (!ArrayUtil.isEmpty(userIdsRemove)) {
            authGroupUserDao.deleteByGroupIdUserIds(groupId, userIdsRemove);
        }

        if (!ArrayUtil.isEmpty(roleIdsRemove)) {
            authGroupRoleDao.deleteByGroupIdRoleIds(groupId, roleIdsRemove);
        }

        if (!ArrayUtil.isEmpty(groupUserAdd)) {
            authGroupUserDao.batchInsert(groupUserAdd);
        }

        if (!ArrayUtil.isEmpty(groupRoleAdd)) {
            authGroupRoleDao.batchInsert(groupRoleAdd);
        }

        // 更新用户组
        if (null != input.getName()) {
            authGroupDao.updateByIdSelective(new AuthGroupEntity(null, input.getName()));
        }

        return RespResult.ok();
    }

    @Transactional
    @Override
    public RespResult delete(Integer id) {
        AuthGroupEntity group = authGroupDao.findById(id);
        if (null == group) {
            return RespResult.secByError(RespCodeEnum.NOT_EXIST.getCode(), "用户组不存在");
        }

        // TODO 是否只能在组里面没有用户情况下才可以删除组

        // 删除 group、group_user、group_role、user_role
        List<AuthGroupUserEntity> groupUserList = authGroupUserDao.findByCondition(new AuthGroupUserEntity(null, id, null), null);
        List<Integer> userIdList = groupUserList.stream().map(AuthGroupUserEntity::getUserId).distinct().collect(Collectors.toList());

        List<AuthGroupRoleEntity> groupRoleList = authGroupRoleDao.findByCondition(new AuthGroupRoleEntity(null, id, null), null);
        List<Integer> roleIdList = groupRoleList.stream().map(AuthGroupRoleEntity::getRoleId).distinct().collect(Collectors.toList());


        authGroupDao.deleteById(id);

        authGroupUserDao.deleteByGroupId(id);

        authGroupRoleDao.deleteByGroupId(id);

        // 一个用户可能有多个组，可能在其他组有分配相同的角色，所以得判断用户在其他组的角色不能删除
        if (!ArrayUtil.isEmpty(userIdList) && !ArrayUtil.isEmpty(roleIdList)) {
            Map<Integer, List<Integer>> userIdRoleIdsMapExcludeCurrentGroup = this.getUserIdRoleIdsMapExcludeCurrentGroup(id, userIdList);
            for (Integer userId : userIdList) {
                List<Integer> needRemoveRoleIds;
                List<Integer> roleIdsInOtherGroup = userIdRoleIdsMapExcludeCurrentGroup.get(userId);
                if (ArrayUtil.isEmpty(roleIdsInOtherGroup)) {
                    needRemoveRoleIds = roleIdList;
                } else {
                    needRemoveRoleIds = roleIdList.stream().filter(roleId -> !roleIdsInOtherGroup.contains(roleId)).collect(Collectors.toList());
                }
                if (!ArrayUtil.isEmpty(needRemoveRoleIds)) {
                    authUserRoleDao.deleteByUserIdsRoleIds(userIdList, needRemoveRoleIds);
                }
            }
        }

        return RespResult.ok();
    }

    @Override
    public List<AuthGroupDetailDto> all() {
        List<AuthGroupEntity> allGroup = authGroupDao.findAll();
        if (ArrayUtil.isEmpty(allGroup)) {
            return new ArrayList<>();
        }
        List<Integer> groupIds = allGroup.stream().map(AuthGroupEntity::getId).collect(Collectors.toList());
        List<AuthGroupUserEntity> groupUserList = authGroupUserDao.findByGroupIds(groupIds);
        List<AuthGroupRoleEntity> groupRoleList = authGroupRoleDao.findByGroupIds(groupIds);

        Map<Integer, List<Integer>> groupIdUserIdsMap = groupUserList.stream().collect(Collectors.groupingBy(AuthGroupUserEntity::getGroupId, Collectors.mapping(AuthGroupUserEntity::getUserId, Collectors.toList())));
        Map<Integer, List<Integer>> groupIdRoleIdsMap = groupRoleList.stream().collect(Collectors.groupingBy(AuthGroupRoleEntity::getGroupId, Collectors.mapping(AuthGroupRoleEntity::getRoleId, Collectors.toList())));

        List<AuthRoleEntity> roleList = authRoleDao.findByIds(groupRoleList.stream().map(AuthGroupRoleEntity::getRoleId).distinct().collect(Collectors.toList()).toArray(), null);

        return allGroup.stream().map(x -> {
            List<AuthRoleDetailDto> roleDetailList = null;
            List<Integer> roleIds = groupIdRoleIdsMap.get(x.getId());
            if (!ArrayUtil.isEmpty(roleIds)) {
                List<AuthRoleEntity> roleListByUser = roleList.stream().filter(roleId -> roleIds.contains(roleId)).collect(Collectors.toList());
                if (!ArrayUtil.isEmpty(roleListByUser)) {
                    roleDetailList = roleListByUser.stream().map(y -> new AuthRoleDetailDto(y.getId(), y.getName(), y.getCreateTime(), y.getUpdateTime(), null)).collect(Collectors.toList());
                }
            }
            return new AuthGroupDetailDto(x.getId(), x.getName(), groupIdUserIdsMap.get(x.getId()), x.getCreateTime(), x.getUpdateTime(), roleDetailList);
        }).collect(Collectors.toList());
    }

    @Override
    public RespResult<AuthGroupDetailDto> read(Integer id) {

        AuthGroupEntity group = authGroupDao.findById(id);
        if (null == group) {
            return RespResult.secByError(RespCodeEnum.NOT_EXIST.getCode(), "用户组不存在");
        }

        List<AuthGroupUserEntity> groupUserList = authGroupUserDao.findByCondition(new AuthGroupUserEntity(null, id, null), null);
        List<AuthGroupRoleEntity> groupRoleList = authGroupRoleDao.findByCondition(new AuthGroupRoleEntity(null, id, null), null);

        List<Integer> userIds = groupUserList.stream().map(AuthGroupUserEntity::getUserId).distinct().collect(Collectors.toList());
        List<Integer> roleIds = groupRoleList.stream().map(AuthGroupRoleEntity::getRoleId).distinct().collect(Collectors.toList());

        List<AuthRoleEntity> roleList = authRoleDao.findByIds(roleIds.toArray(), null);

        List<AuthRoleDetailDto> roleDetailList = null;

        if (!ArrayUtil.isEmpty(roleList)) {
            roleDetailList = roleList.stream().map(x -> new AuthRoleDetailDto(x.getId(), x.getName(), x.getCreateTime(), x.getUpdateTime(), null)).collect(Collectors.toList());
        }

        return RespResult.ok(new AuthGroupDetailDto(group.getId(), group.getName(), userIds, group.getCreateTime(), group.getUpdateTime(), roleDetailList));

    }

    private Map<Integer, List<Integer>> getUserIdRoleIdsMapExcludeCurrentGroup(Integer cuurentGroupId, List<Integer> userIds) {
        // 查询关联用户的组用户信息、组角色
        List<AuthGroupUserEntity> groupUsersByRelation = authGroupUserDao.findByUserIds(userIds);
        List<AuthGroupRoleEntity> groupRolesByRelation = authGroupRoleDao.findByGroupIds(groupUsersByRelation.stream().map(AuthGroupUserEntity::getGroupId).distinct().collect(Collectors.toList()));

        // 得到用户对应的组列表
        Map<Integer, List<Integer>> userIdGrouoIdsMapByRelation = groupUsersByRelation.stream().collect(Collectors.groupingBy(AuthGroupUserEntity::getUserId, Collectors.mapping(AuthGroupUserEntity::getGroupId, Collectors.toList())));

        // 得到组对应的角色列表
        Map<Integer, List<Integer>> groupIdRoleIdsMapByRelation = groupRolesByRelation.stream().collect(Collectors.groupingBy(AuthGroupRoleEntity::getGroupId, Collectors.mapping(AuthGroupRoleEntity::getRoleId, Collectors.toList())));

        return this.getUserIdRoleIdsMapExcludeCurrentGroup(cuurentGroupId, userIdGrouoIdsMapByRelation, groupIdRoleIdsMapByRelation);
    }

    private Map<Integer, List<Integer>> getUserIdRoleIdsMapExcludeCurrentGroup(Integer cuurentGroupId, Map<Integer, List<Integer>> userIdGrouoIdsMapByRelation, Map<Integer, List<Integer>> groupIdRoleIdsMapByRelation) {

        // 筛选出用户在非当前组所拥有的角色
        Map<Integer, List<Integer>> userIdRoleIdsMapExcludeCurrentGroup = new HashMap<>();
        for (Integer userId : userIdGrouoIdsMapByRelation.keySet()) {
            List<Integer> grougIdsByUserId = userIdGrouoIdsMapByRelation.get(userId);
            for (Integer groupIdByUserId : grougIdsByUserId) {
                if (groupIdByUserId.equals(cuurentGroupId)) {
                    continue;
                }
                List<Integer> roleIdsByGroupId = groupIdRoleIdsMapByRelation.get(groupIdByUserId);
                if (ArrayUtil.isEmpty(roleIdsByGroupId)) {
                    continue;
                }
                List<Integer> roleIdsByUserIdRelation = userIdRoleIdsMapExcludeCurrentGroup.get(userId);
                if (null == roleIdsByUserIdRelation) {
                    roleIdsByUserIdRelation = new ArrayList<>();
                }
                roleIdsByUserIdRelation.addAll(roleIdsByGroupId);
                roleIdsByUserIdRelation = roleIdsByUserIdRelation.stream().distinct().collect(Collectors.toList());
                userIdRoleIdsMapExcludeCurrentGroup.put(userId, roleIdsByUserIdRelation);
            }
        }
        return userIdRoleIdsMapExcludeCurrentGroup;
    }

}
